//
//  BabyPhotoTableList.m
//  BabyHelper
//
//  Created by  thomas on 11-7-17.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BabyPhotoCategoryList.h"


@implementation BabyPhotoCategoryList


#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
	self.title = NSLocalizedString(@"Album", @"");
    [super viewDidLoad];

	//set background image
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
	self.view.backgroundColor = [UIColor clearColor];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];
	
	UIBarButtonItem *editItem = [[UIBarButtonItem alloc]
								   initWithTitle:NSLocalizedString(@"Edit", @"")
								   style:UIBarButtonItemStyleBordered 
								   target:self
								   action:@selector(toggleEdit)		
								   ];
	self.navigationItem.leftBarButtonItem = editItem;
	[editItem release];
	
	UIBarButtonItem *addItem = [[UIBarButtonItem alloc]
								initWithTitle:NSLocalizedString(@"Add Photo",@"")
								style:UIBarButtonItemStyleBordered 
								target:self
								action:@selector(toggleAdd)		
								];
	self.navigationItem.rightBarButtonItem = addItem;
	[addItem release];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    photoAlbumList = [[NSMutableArray alloc]init];
	dateFormatter  = [[NSDateFormatter alloc] init];
	[dateFormatter setDateFormat:DATE_TIME_FORMAT];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	//set navigation background color
	self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:navBarRed green:navBarGreen blue:navBarBlue alpha:navBarAlpha];	
	
	Dao *dao = [Dao getInstance];
	[photoAlbumList removeAllObjects];
	[dao QueryPhotoCategoryEntryWith:@"where ID > 0 ORDER BY ID ASC" records:photoAlbumList];
	[self.tableView reloadData];
	
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSString *)tableView:(UITableView *)tv titleForHeaderInSection:(NSInteger)section
{
	NSString *title = nil;
	
	switch(section){
		case 0:
			//title = NSLocalizedString(@"Favourite Album", @"");
			break;
		case 1:
			title = NSLocalizedString(@"Photo Albums", @"");
			break;			
		default:
			break;
	}
	return title;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (section == 0)
		return 4;
	if (section == 1)
		return [photoAlbumList count];
	else 
		return 0;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	if(	indexPath.section == 0)
	{
		switch (indexPath.row) {
			case 0:
				cell.textLabel.text = NSLocalizedString(@"Grow album", @"");
				cell.detailTextLabel.text = NSLocalizedString(@"Baby grow photo album", @""); 
				cell.detailTextLabel.textColor = [UIColor colorWithRed:commentsTextRed green:commentsTextGreen blue:commentsTextBlue alpha:commentsTextAlpha];			
				cell.imageView.image = [UIImage imageNamed:@"babyphoto_grow"];
				break;
			case 1:
				cell.textLabel.text = NSLocalizedString(@"Other photo album", @"");
				cell.detailTextLabel.text = NSLocalizedString(@"Take baby photo anywhere", @""); 
				cell.detailTextLabel.textColor = [UIColor colorWithRed:commentsTextRed green:commentsTextGreen blue:commentsTextBlue alpha:commentsTextAlpha];						
				cell.imageView.image = [UIImage imageNamed:@"camera"];
				break;
			case 2:
				cell.textLabel.text = NSLocalizedString(@"Photo in Map", @"");
				cell.detailTextLabel.text = NSLocalizedString(@"Display baby photo in map", @""); 
				cell.detailTextLabel.textColor = [UIColor colorWithRed:commentsTextRed green:commentsTextGreen blue:commentsTextBlue alpha:commentsTextAlpha];						
				cell.imageView.image = [UIImage imageNamed:@"photo_map"];
				break;			
			case 3:
				cell.textLabel.text = NSLocalizedString(@"Share Photo", @"");
				cell.detailTextLabel.text = NSLocalizedString(@"Share Photo to my friends", @""); 
				cell.detailTextLabel.textColor = [UIColor colorWithRed:commentsTextRed green:commentsTextGreen blue:commentsTextBlue alpha:commentsTextAlpha];						
				cell.imageView.image = [UIImage imageNamed:@"Sina_32x32"];
				break;			
			default:
				break;
		}
	}
	
	if(	indexPath.section == 1)
	{
		PhotoAlbumEntry *entry = [photoAlbumList objectAtIndex:indexPath.row];
		cell.textLabel.text       = entry.name;
		cell.detailTextLabel.text = [dateFormatter stringFromDate:entry.timestamp];		
	}	
	cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


-(void)toggleAdd{
	EditBabyPhotoView *childView = [[EditBabyPhotoView alloc] initWithNibName:@"EditBabyPhoto" bundle:nil];
	childView.currentBabyPhotoEntry = nil;
	[self.navigationController pushViewController:childView animated:YES];
	[childView release];
}

-(void)toggleEdit{
	BabyPhotoListView *childView = [[BabyPhotoListView alloc] initWithNibName:@"BabyPhotoListVIew" bundle:nil];
	//EditPhotoAlbumListView *childView = [[EditPhotoAlbumListView alloc] initWithNibName:@"EditPhotoCategoryListView" bundle:nil]; 
	[self.navigationController pushViewController:childView animated:YES];
	[childView release];
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	if(	indexPath.section == 0)
	{
		if( indexPath.row == 0 || indexPath.row == 1)
		{
			NSString *whereClause;
			if(indexPath.row == 0)
				whereClause = @"where THUMPATH != 'null' and babygrowID > 0 ORDER BY TIMESTAMP DESC";
			if(indexPath.row == 1)
				whereClause = @"where THUMPATH != 'null' and categoryid = 0 and babygrowID <= 0 ORDER BY TIMESTAMP DESC";

			[PhotoSet updateBabyPhotoSet:whereClause photoCategory:-1];
			BabyGrowPhotoView *childView = [[BabyGrowPhotoView alloc]init];
			childView.whereClause   = whereClause;
			childView.photoCategory = -1;
			[self.navigationController pushViewController:childView animated:YES];
			[childView release];
		}
		
		if(indexPath.row == 2)
		{		
			PhotoMapView *childView = [[PhotoMapView alloc]init];
			[self.navigationController pushViewController:childView animated:YES];
			[childView UpdateAnnotations];
			[childView release];
		}
		
		if(indexPath.row == 3)
		{
			BabyPhotoListView *childView = [[BabyPhotoListView alloc] initWithNibName:@"BabyPhotoListVIew" bundle:nil]; 
			NSString *whereClause = @"WHERE categoryID = 0 and thumpath != 'null' ORDER BY TIMESTAMP DESC";
			childView.whereClause   = whereClause;
			childView.viewMode  = ShareMode;
			[self.navigationController pushViewController:childView animated:YES];
			[childView release];
		}
	}
	
	if(	indexPath.section == 1)
	{
		PhotoAlbumEntry *entry = [photoAlbumList objectAtIndex:indexPath.row];
		NSString *whereClause = [NSString stringWithFormat:@"where THUMPATH != 'null' and categoryid = '%d' and babygrowID <= 0 ORDER BY TIMESTAMP DESC",entry.index];
		[PhotoSet updateBabyPhotoSet:whereClause photoCategory:entry.index];
		BabyGrowPhotoView *childView = [[BabyGrowPhotoView alloc]init];
		childView.whereClause   = whereClause;
		childView.photoCategory = entry.index;
		[self.navigationController pushViewController:childView animated:YES];
		[childView release];
	}
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}
@end

